package com.angcyo.selenium.auto.action

import com.angcyo.library.ex.sleep
import com.angcyo.library.ex.subEnd
import com.angcyo.selenium.auto.BaseControl
import com.angcyo.selenium.auto.parse.HandleResult
import com.angcyo.selenium.bean.ActionBean
import org.openqa.selenium.WebElement

/**
 * Email:angcyo@126.com
 * @author angcyo
 * @date 2021/01/14
 */
class SleepAction : BaseAction() {
    override fun interceptAction(control: BaseControl, action: String?): Boolean {
        return action?.startsWith(Action.ACTION_SLEEP) == true
    }

    override fun runAction(
        control: BaseControl,
        element: WebElement,
        actionBean: ActionBean,
        action: String
    ): HandleResult {
        return element.actionResult(control) { elementStr ->
            val time = control._autoParse.parseTime(action.subEnd(Action.ARG_SPLIT))
            if (time > 0) {
                control.logAction?.invoke("开始休眠:$time")
                sleep(time)
            }
            (time > 0) to "休眠[${time}]"
        }
    }
}